package com.cyc.gulimall.order.interceptor;

import com.cyc.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> loginUser=new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        MemberResponseVo loginUser1 = (MemberResponseVo) session.getAttribute("loginUser");

        if (loginUser1!=null){
            loginUser.set(loginUser1);
            return true;
        }else {
            session.setAttribute("msg","请先登陆");
            response.sendRedirect("http://localhost:20000/login.html");
            return false;
        }


    }
}
